package com.zhugang.week03;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @program algorithms
 * @description: myStack
 * @author: chanzhugang
 * @create: 2022/06/06 17:07
 */
public class MyStack3 {

    /**
     * 双端队列
     */
    Deque<Integer> deque;

    /**
     * 225. 用队列实现栈:
     * push：倒腾数据：新数据插入队尾，之前的数据从对头取出重新插入队尾
     */
    public MyStack3() {
        deque = new LinkedList<>();
    }


    public void push(int x) {
        int n = deque.size();
        deque.offer(x);
        for (int i = 0; i < n; i++) {
            // 从对头取出重新放队尾
            deque.offer(deque.poll());
        }
    }

    public int pop() {
        return deque.poll();
    }

    public int top() {
        return deque.peek();
    }

    public boolean empty() {
        return deque.isEmpty();
    }
}